package com.wxapp.sale.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.wxapp.common.constant.base.IBaseService;
import com.wxapp.common.constant.pojo.vm.ResultVm;
import com.wxapp.common.constant.pojo.vo.UserVo;
import com.wxapp.common.util.copy.CopyUtil;
import com.wxapp.common.util.random.RandomUtil;
import com.wxapp.sale.api.pojo.entity.GoodsLabelDO;
import com.wxapp.sale.api.pojo.vm.goodsLabel.PGoodsLabelSelectVm;
import com.wxapp.sale.service.mapper.GoodsLabelMapper;
import com.wxapp.sale.service.service.GoodsLabelService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lgl
 * @Description
 * @Datetime 2023/4/23 16:09
 * @Modified By
 */
@Service
@RequiredArgsConstructor
public class GoodsLabelServiceImpl extends IBaseService implements GoodsLabelService {
    private final GoodsLabelMapper goodsLabelMapper;

    @Override
    public ResultVm<String> save(UserVo userVo, String name) {
        LambdaQueryWrapper<GoodsLabelDO> query = new LambdaQueryWrapper<>();
        query.eq(GoodsLabelDO::getName, name).eq(GoodsLabelDO::getStatus, true);
        var exist = goodsLabelMapper.isExist(query);
        if (exist) {
            return error("已经存在该标签");
        }
        GoodsLabelDO goodsLabel=new GoodsLabelDO();
        goodsLabel.setName(name);
        goodsLabel.setCode(RandomUtil.getAllRandom(6));
        goodsLabel.setCreateBy(userVo.getId());
        goodsLabelMapper.insert(goodsLabel);
        return success();
    }

    /**
     *   删除标签
     *
     * @Param:
     * @Author: lgl
     * @Date 2023/4/24 11:34
     */
    @Override
    public ResultVm<String> delete(UserVo userVo, List<String> code) {
        LambdaUpdateWrapper<GoodsLabelDO> query = new LambdaUpdateWrapper<>();
        query.eq(GoodsLabelDO::getStatus, true).in(GoodsLabelDO::getCode, code).set(GoodsLabelDO::getStatus, false);
        goodsLabelMapper.update(null, query);
        return success();
    }

    /**
     * 获取列表
     *
     * @return
     */
    @Override
    public ResultVm<List<PGoodsLabelSelectVm>> select(UserVo userVo) {
        LambdaQueryWrapper<GoodsLabelDO> query = new LambdaQueryWrapper<>();
        query.eq(GoodsLabelDO::getStatus, true)
                .eq(GoodsLabelDO::getCreateBy,userVo.getId());
        var labelDOS = goodsLabelMapper.selectList(query);
        return success(CopyUtil.copyListNew(labelDOS, PGoodsLabelSelectVm::new, null));
    }


}
